﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;
using System.ServiceModel.Syndication;
using System.Net;
using System.Xml;
using System.Diagnostics;


namespace KepoinYT
{
    public partial class UserControl1 : UserControl
    {

       
        public UserControl1()
        {
            InitializeComponent();
        }
        
        
        // METHOD -------------------------------------------------------------------------------------------
        void favoritvid()
        {
            string url = "http://gdata.youtube.com/feeds/api/standardfeeds/ID/most_popular_Music?time=today";




            //ambil semua data
            this.richTextBox1.Text = string.Empty;

            SyndicationFeed uploadFeed;


            //webclient proxy
            WebClient client = new WebClient();
            client.Proxy = WebRequest.DefaultWebProxy;
            client.Proxy.Credentials = CredentialCache.DefaultCredentials;

            //baca feed pake xml reader
            using (XmlReader reader = XmlReader.Create(client.OpenRead(url)))
            {
                //load feed Syndication
                uploadFeed = SyndicationFeed.Load(reader);
            }
            //pake feed

            this.richTextBox1.Text += "Video terpopular hari ini : \n\n";
            int a = 1;
            foreach (SyndicationItem entry in uploadFeed.Items)
            {
                this.richTextBox1.Text += a + " .  "+ entry.Title.Text + "\n" + entry.Id + "\n"+ entry.PublishDate +"\n\n";
                a++;
            }
        }
        //-----------------------------------------------------------------------------------------

        //AUTOLOAD TOP FAVORITE VIDEO--------------------------------------------------------------
        private void UserControl1_Load(object sender, EventArgs e)
        {
            favoritvid();

        }

        //Tombol profile----------------------------------------------------------------------------
        private void button1_Click(object sender, EventArgs e)
        {
            string username = this.textBox1.Text;

            this.richTextBox1.Text = string.Empty;
            // ambil data 
            YouTubeService service = new YouTubeService("MytestAlication");
            string feedUrl = "https://gdata.youtube.com/feeds/api/users/" + username;
            ProfileEntry profile = (ProfileEntry)service.Get(feedUrl);
            //tampilkan
            this.richTextBox1.Text += "\nUsername   :   " + profile.UserName;
            this.richTextBox1.Text += "\nAge        :   " + profile.Age;
            this.richTextBox1.Text += "\nGender     :   " + profile.Gender;
            this.richTextBox1.Text += "\nSingle?    :   " + profile.Relationship;

            this.richTextBox1.Text += "\nBooks      :   " + profile.Books;
            this.richTextBox1.Text += "\nCompany    :   " + profile.Company;
            this.richTextBox1.Text += "\nDescription:   " + profile.Summary.Text;
            this.richTextBox1.Text += "\nHobbies    :   " + profile.Hobbies;
            this.richTextBox1.Text += "\nHometown   :   " + profile.Hometown;
            this.richTextBox1.Text += "\nLocation   :   " + profile.Location;
            this.richTextBox1.Text += "\nMovies     :   " + profile.Movies;
            this.richTextBox1.Text += "\nMusic      :   " + profile.Music;
            this.richTextBox1.Text += "\nJob        :   " + profile.Occupation;
            this.richTextBox1.Text += "\nSchool     :   " + profile.School;
            this.richTextBox1.Text += "\nSubscriber :   " + profile.Statistics.SubscriberCount;
            this.richTextBox1.Text += "\nView       :   " + profile.Statistics.ViewCount;
            this.richTextBox1.Text += "\nWatch      :   " + profile.Statistics.WatchCount;
        }
        
        
        // tombol koleksi video --------------------------------------------------------------------------
        private void button2_Click(object sender, EventArgs e)
        {
            string username = this.textBox1.Text;

            string url = "https://gdata.youtube.com/feeds/api/users/" + username + @"/uploads";

            //ambil semua data
            this.richTextBox1.Text = string.Empty;

            SyndicationFeed uploadFeed;


            //webclient proxy
            WebClient client = new WebClient();
            client.Proxy = WebRequest.DefaultWebProxy;
            client.Proxy.Credentials = CredentialCache.DefaultCredentials;

            //baca feed pake xml reader
            using (XmlReader reader = XmlReader.Create(client.OpenRead(url)))
            {
                //load feed Syndication
                uploadFeed = SyndicationFeed.Load(reader);
            }
            //pake feed
            
            foreach (SyndicationItem entry in uploadFeed.Items)
            {
                this.richTextBox1.Text += entry.Title.Text +"\n" + entry.Id + "\n"+entry.PublishDate+"\n\n\n";   
            }


        }

        //tombol a-z-----------------------------------------------------------------------------
        private void button3_Click(object sender, EventArgs e)
        {
           
            string[] array = new string[50];
            int i = 0;

            string username = this.textBox1.Text;

            string url = "https://gdata.youtube.com/feeds/api/users/" + username + @"/uploads";
            //ambil semua data
            this.richTextBox1.Text = string.Empty;

            SyndicationFeed uploadFeed;


            //webclient proxy
            WebClient client = new WebClient();
            client.Proxy = WebRequest.DefaultWebProxy;
            client.Proxy.Credentials = CredentialCache.DefaultCredentials;

            //baca feed pake xml reader
            using (XmlReader reader = XmlReader.Create(client.OpenRead(url)))
            {
                //load feed Syndication
                uploadFeed = SyndicationFeed.Load(reader);
            }
            //pake feed
            
            foreach (SyndicationItem entry in uploadFeed.Items)
            {

                array[i] = entry.Title.Text + "\n" + entry.Id + "\n\n\n";
                i++;
            }

            Array.Sort(array);
           
            foreach (string s in array)
            {
                
                this.richTextBox1.Text += s;
              
            }
            
            
        }

        //tombol z-a ----------------------------------------------------------------------------
        private void button4_Click(object sender, EventArgs e)
        {
            string[] array = new string[50];
            int i = 0;

            string username = this.textBox1.Text;

            string url = "https://gdata.youtube.com/feeds/api/users/" + username + @"/uploads";




            //ambil semua data
            this.richTextBox1.Text = string.Empty;

            SyndicationFeed uploadFeed;


            //webclient proxy
            WebClient client = new WebClient();
            client.Proxy = WebRequest.DefaultWebProxy;
            client.Proxy.Credentials = CredentialCache.DefaultCredentials;

            //baca feed pake xml reader
            using (XmlReader reader = XmlReader.Create(client.OpenRead(url)))
            {
                //load feed Syndication
                uploadFeed = SyndicationFeed.Load(reader);
            }
            //pake feed

            foreach (SyndicationItem entry in uploadFeed.Items)
            {

                array[i] = entry.Title.Text + "\n" + entry.Id + "\n\n\n";
                i++;
            }



            var result = from element in array
                         orderby element descending
                         select element;

           
            foreach (var element in result)
            {
                this.richTextBox1.Text += element;
              
            }

        }

        //playlist
        private void button5_Click(object sender, EventArgs e)
        {
            string username = this.textBox1.Text;

            string url = "https://gdata.youtube.com/feeds/api/users/" + username + @"/playlists";



            //ambil semua data
            this.richTextBox1.Text = string.Empty;

            SyndicationFeed uploadFeed;


            //webclient proxy
            WebClient client = new WebClient();
            client.Proxy = WebRequest.DefaultWebProxy;
            client.Proxy.Credentials = CredentialCache.DefaultCredentials;

            //baca feed pake xml reader
            using (XmlReader reader = XmlReader.Create(client.OpenRead(url)))
            {
                //load feed Syndication
                uploadFeed = SyndicationFeed.Load(reader);
            }
            //pake feed
            foreach (SyndicationItem entry in uploadFeed.Items)
            {

                this.richTextBox1.Text += entry.Title.Text + "\n" + entry.Id + "\n\n";

            }
           
        }
    
        //terlama ------------------------------------------------------------------------------------
         private void button6_Click_1(object sender, EventArgs e)
         {
             string username = this.textBox1.Text;
             string url = "https://gdata.youtube.com/feeds/api/users/" + username + @"/uploads";




             //ambil semua data
             this.richTextBox1.Text = string.Empty;

             SyndicationFeed uploadFeed;


             //webclient proxy
             WebClient client = new WebClient();
             client.Proxy = WebRequest.DefaultWebProxy;
             client.Proxy.Credentials = CredentialCache.DefaultCredentials;

             //baca feed pake xml reader
             using (XmlReader reader = XmlReader.Create(client.OpenRead(url)))
             {
                 //load feed Syndication
                 uploadFeed = SyndicationFeed.Load(reader);
             }
             //pake feed
             int no = 0;
             string[] array = new string[25];
             string[] id = new string[25];
             foreach (SyndicationItem entry in uploadFeed.Items)
             {

                 array[no] = entry.Title.Text;
                 id[no] = entry.Id;
                
             }

             this.richTextBox1.Text = array[0] + "\n" + id[0];
          
         }
        
    }
}

